import Vue from "vue"
import "./index.less";
// import viewportUnitsBuggyfill from "viewport-units-buggyfill"
// viewportUnitsBuggyfill.init({
//     hacks: window.viewportUnitsBuggyfillHacks
// });
import { getList, newBoxList } from "../../config/api/index"
import { throttle } from "../../util/index";
new Vue({
    el: "#app",
    data() {
        return {
            info: {
                params: {
                    page: 0,
                    pageSize: 10
                },
                list: [],
                isLoading: false,
                isFinish: false
            },
            goodList: [],
            wxShow: false
        }
    },
    methods: {
        is_weixn() {
            let ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                this.wxShow = true;
            } else {
                this.wxShow = false;
            }
        },
        // 获取活动图
        async getGoodList() {
            const res = await newBoxList();
            console.log('res', res)
            if (res.code == 0) {
                this.goodList = res.data.slice(0, 6);
            } else {
                alert(res.msg);
            }
        },
        goBox() {
            let box_id = this.goodList[0].box_id;
            this.goApp(box_id);
        },
        goApp(box_id) {
            location.href=`/h5/pages/magic-box/detail/detail?code=${this.code}&&magicBoxId=${box_id}`
            // location.href = `magicBox://pages/magic-box/detail/detail?code=${this.code}&&magicBoxId=${box_id}`;
            // setTimeout(() => {
            //     let hidden = window.document.hidden || window.document.webkitHidden || window.document
            //         .mozHidden || window.document.msHidden;
            //     if (typeof (hidden) == "undefined" || hidden == false) {
            //         window.location =  `../download/download.html`;
            //     }
            // }, 3000)
        },
        async api(info) {
            if (info.isFinish || info.isLoading) {
                // console.log("info.isLoading",info.isLoading)
                return
            }
            info.isLoading = true;
            let filterIds = this.info.list.map(item => item.id).join(",");
            const params = Object.assign({}, info.params, { page: info.params.page + 1, display: filterIds })
            try {
                const res = await getList(params);
                console.log('res', res)
                if (res.code == 0) {
                    if (Array.isArray(res.data)) {
                        this.info.params.page++
                        this.info.list = [...this.info.list, ...res.data]
                        //没有更多
                        if (!res.data.length) {
                            this.info.isFinish = true
                        }
                    } else if (res.data.list) {
                        this.info.params.page++
                        this.info.list = [...this.info.list, ...res.data.list]
                        //没有更多
                        if (!res.data.list.length) {
                            this.info.isFinish = true;
                        }
                    }
                } else {
                    alert(res.msg);
                }
            } catch (error) {
                console.log("error", error)
            }
            info.isLoading = false
        },
        reachBottom(element) {
            const scrollHeight = element.scrollHeight
            const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
            const clientHeight = element.clientHeight
            if (scrollTop + clientHeight + 80 > scrollHeight) {
                this.throttleApi(this.info)
            }
        },
        getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        }
    },
    async created() {
        this.code = this.getUrlParam('code');
        this.is_weixn();
        this.getGoodList();
        await this.api(this.info)
        this.throttleApi = throttle(this.api, 300)
        const element = document.documentElement || document.body
        window.onscroll = this.reachBottom.bind(this, element)

    }
})
